/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.UserManager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowInsets;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.ecarx.systemui.plugin.psd.PSDMainActivity;
import com.ecarx.systemui.plugin.psd.PsdBarsManager;
import com.ecarx.systemui.plugin.utils.DensityUtils;
import com.ecarx.systemui.plugin.utils.LogUtils;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "XCSystemUIPluginTag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout llRoot = findViewById(R.id.tv_ll);
        ViewGroup.LayoutParams paramsStatusBar =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        DensityUtils.dp2px(94));
        View statusBarView = StatusBar.init(this).getRootView();
        ViewParent parent = statusBarView.getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(statusBarView);
        }
        llRoot.addView(statusBarView, 0, paramsStatusBar);

        ViewGroup.LayoutParams paramsNaviBar =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        DensityUtils.dp2px(136));
        View navigationBarView = NavigationBar.init(this).getRootView();
        ViewParent parent2 = navigationBarView.getParent();
        if (parent2 != null) {
            ((ViewGroup) parent2).removeView(navigationBarView);
        }
        llRoot.addView(navigationBarView, 2, paramsNaviBar);


    }


    private void startPsdView() {
        Intent intent = new Intent();
        intent.setPackage(getPackageName());
        intent.setClass(this, PSDMainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startActivity(intent, ActivityOptions.makeBasic().setLaunchDisplayId(2).toBundle());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//                    getWindow().getDecorView().getWindowInsetsController().hide(WindowInsets.Type.systemBars());
//                    getWindow().getDecorView().getWindowInsetsController().hide(WindowInsets.Type.navigationBars());
                    boolean visible = getWindow().getDecorView().getRootWindowInsets().isVisible(WindowInsets.Type.navigationBars());
                    LogUtils.d(TAG, "csd visible:" + visible);

                }
            }
        });

//        startPsdView();
        PsdBarsManager.getInstance().showPsdBars(PsdBarsManager.ESystemBarType.NAVIGATION_BAR);
    }

    @Override
    protected void onStop() {
        super.onStop();
        PsdBarsManager.getInstance().removePsdBars(PsdBarsManager.ESystemBarType.NAVIGATION_BAR);
    }

    public static View createNavigationBarWidget(Context context, Bundle bundle) {
        LogUtils.d(TAG, "createNavigationBarWidget");
        return NavigationBar.init(context).getRootView();
    }

    public static View createSystemUIWidget(Context context, Bundle bundle) {
        LogUtils.d(TAG, "createSystemUIWidget");
        return StatusBar.init(context).getRootView();
    }
}